#include "stdafx.h"
#include "keyboardplayercontrol.h"
#include <external/external.h>
#include <model/mage.h>

KeyboardPlayerControl::KeyboardPlayerControl():
    _leftPressed(0),
    _rightPressed(0),
    _stopPressed(0),
    _forwardPressed(0),
    _backPressed(0)
{
    qDebug() << "Keyboard controller initialized.";
    qDebug() << "  hotkeys:";
    qDebug() << "    \"W\", Up - increase speed";
    qDebug() << "    \"S\", Down - decrease speed";
    qDebug() << "    Space - stop unit";
    qDebug() << "    \"D\", Right - rotate right";
    qDebug() << "    \"A\", Left - rotate left";
}

KeyboardPlayerControl::~KeyboardPlayerControl()
{
}

void KeyboardPlayerControl::process(IWorld *world) const
{
    ISelfControl *self = world->getSelfControl();
    if (_stopPressed) {
        self->boost(-self->speed());
    } else if (_forwardPressed) {
        self->boost(self->getMaxBoostDelta());
    } else if (_backPressed) {
        self->boost(-self->getMaxBoostDelta());
    }
    if (_leftPressed) {
        self->rotate(self->getMaxRotationDelta());
    } else if (_rightPressed) {
        self->rotate(-self->getMaxRotationDelta());
    }
}

void KeyboardPlayerControl::accept(QKeyEvent *event, bool isPressEvent)
{
    switch (event->key())
    {
        case Qt::Key_Space:
            _stopPressed = isPressEvent;
            break;
        case Qt::Key_W:
        case Qt::Key_Up:
            _forwardPressed = isPressEvent;
            break;
        case Qt::Key_S:
        case Qt::Key_Down:
            _backPressed = isPressEvent;
            break;
        case Qt::Key_D:
        case Qt::Key_Right:
            _rightPressed = isPressEvent;
            break;
        case Qt::Key_A:
        case Qt::Key_Left:
            _leftPressed = isPressEvent;
            break;
    }
}
